const mongoose = require('mongoose');

// 字典子项 Schema
const dictItemSchema = new mongoose.Schema({
  id: {
    type: String,
    required: true,
    unique: true
  },
  dictId: {
    type: String,
    required: true,
    index: true
  },
  label: {
    type: String,
    required: true
  },
  value: {
    type: String,
    required: true
  },
  sort: {
    type: Number,
    default: 0
  },
  status: {
    type: Boolean,
    default: true
  },
  remark: String
}, {
  timestamps: true
});

// 添加复合索引以提高查询性能
dictItemSchema.index({ dictId: 1, value: 1 }, { unique: true });

// 字典主表 Schema
const dictSchema = new mongoose.Schema({
  id: {
    type: String,
    required: true,
    unique: true
  },
  name: {
    type: String,
    required: true
  },
  code: {
    type: String,
    required: true,
    unique: true
  },
  sort: {
    type: Number,
    default: 0
  },
  status: {
    type: Boolean,
    default: true
  },
  remark: String
}, {
  timestamps: true
});

// 添加索引
dictSchema.index({ code: 1 });

const Dict = mongoose.model('Dict', dictSchema);
const DictItem = mongoose.model('DictItem', dictItemSchema);

module.exports = {
  Dict,
  DictItem
}; 